package com.dgz.system.service.question.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.dgz.common.core.constants.Constants;
import com.dgz.common.core.domain.TableDataInfo;
import com.dgz.common.core.enums.ResultCode;
import com.dgz.common.security.exception.ServiceException;
import com.dgz.system.domain.exam.Exam;
import com.dgz.system.domain.question.Question;
import com.dgz.system.domain.question.dto.QuestionAddDto;
import com.dgz.system.domain.question.dto.QuestionEditDto;
import com.dgz.system.domain.question.dto.QuestionQueryDto;
import com.dgz.system.domain.question.es.QuestionES;
import com.dgz.system.domain.question.vo.QuestionDetailVO;
import com.dgz.system.domain.question.vo.QuestionVO;
import com.dgz.system.elasticsearch.QuestionRepository;
import com.dgz.system.manager.QuestionCacheManager;
import com.dgz.system.mapper.question.QuestionMapper;
import com.dgz.system.service.question.IQuestionService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.Collection;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * Created with IntelliJ IDEA.
 *
 * @Author: DongGuoZhen
 * @Date: 2025/04/03/14:13
 * @Description:
 */
@Service
public class QuestionServiceImpl implements IQuestionService {

    @Autowired
    private QuestionMapper questionMapper;

    @Autowired
    private QuestionRepository questionRepository;

    @Autowired
    private QuestionCacheManager questionCacheManager;

    @Override
    public List<QuestionVO> list(QuestionQueryDto questionQueryDto) {
        String excludeIdStr = questionQueryDto.getExcludeIdStr();
        if(StrUtil.isNotEmpty(excludeIdStr)) {
            String[] excludeIdArr = excludeIdStr.split(Constants.SPLIT_SEM);
            Set<Long> excludeIsSet = Arrays
                    .stream(excludeIdStr.split(";"))
                    .map(Long::valueOf)
                    .collect(Collectors.toSet());
            questionQueryDto.setExcludeIdSet(excludeIsSet); // 要排除的题目id
        }

        PageHelper.startPage(questionQueryDto.getPageNum(), questionQueryDto.getPageSize());
        return questionMapper.selectQuestionList(questionQueryDto);
    }

    @Override
    public boolean add(QuestionAddDto questionAddDto) {
        List<Question> questions = questionMapper.selectList(new LambdaQueryWrapper<Question>()
                .eq(Question::getTitle, questionAddDto.getTitle()));
        if(CollectionUtil.isNotEmpty(questions)) {
            throw new ServiceException(ResultCode.FAILED_ALREADY_EXISTS);
        }
        Question question = new Question();
        BeanUtil.copyProperties(questionAddDto, question); // 将questionAddDto中的属性复制到question中
        int insert = questionMapper.insert(question);
        if(insert <= 0) {
            return false;
        }
        QuestionES questionES = new QuestionES();
        BeanUtil.copyProperties(question, questionES);
        questionRepository.save(questionES);
        questionCacheManager.addCache(question.getQuestionId());
        return true;
    }

    @Override
    public QuestionDetailVO detail(Long questionId) {
        Question question = getQuestion(questionId);
        QuestionDetailVO questionDetailVO = new QuestionDetailVO();
        BeanUtil.copyProperties(question, questionDetailVO);
        return questionDetailVO;
    }

    @Override
    public int edit(QuestionEditDto questionEditDto) {
        Question oldQuestion = getQuestion(questionEditDto.getQuestionId());
        oldQuestion.setTitle(questionEditDto.getTitle());
        oldQuestion.setDifficulty(questionEditDto.getDifficulty());
        oldQuestion.setTimeLimit(questionEditDto.getTimeLimit());
        oldQuestion.setSpaceLimit(questionEditDto.getSpaceLimit());
        oldQuestion.setContent(questionEditDto.getContent());
        oldQuestion.setQuestionCase(questionEditDto.getQuestionCase());
        oldQuestion.setDefaultCode(questionEditDto.getDefaultCode());
        oldQuestion.setMainFuc(questionEditDto.getMainFuc());
        QuestionES questionES = new QuestionES();
        BeanUtil.copyProperties(oldQuestion, questionES);
        questionRepository.save(questionES);
        return questionMapper.updateById(oldQuestion);
    }

    @Override
    public int delete(Long questionId) {
        getQuestion(questionId);
        questionRepository.deleteById(questionId);
        questionCacheManager.deleteCache(questionId);
        return questionMapper.deleteById(questionId);
    }


    private Question getQuestion(Long questionId) {
        Question question = questionMapper.selectById(questionId);
        if(question == null) {
            throw new ServiceException(ResultCode.FAILED_NOT_EXISTS);
        }
        return question;
    }
}
